<?php
require_once 'Tedi/SourceMinifier/Interface.php';

/**
 * @link http://code.google.com/p/minify/source/browse/trunk/min/lib/Minify/YUICompressor.php
 */
class Tedi_SourceMinifier_JS implements Tedi_SourceMinifier_Interface
{
	protected $_JSMin  = null;
	
	protected $_source     = null;
	protected $_isMinified = false;
	
	
	public static function fromString($string)
	{
		return new self($string);
	}
	
	public static function fromFile($fileName)
	{
		$fileContents = '';
		
		return self::fromString($fileContents);
	}
	
	public function __construct($source)
	{
		$this->_source = $source;
	}
	
	public function getMinified()
	{
		return $this->_minify();
	}
	
	public function saveFile($fileName)
	{
		return true;
	}
	
	/*
	 * Force browser download window
	 * 
	 * @param string $fileName defines download filename
	 */
	public function download($fileName = '')
	{
		$this->_minify();
		
		//header('Content-Type: text/javascript');
		//header('Content-Length: '.mb_strlen($this->_source));
	}
	
	public function free()
	{
		foreach($this as $attr)
		{
			Tedi_Memory::free($this->$attr);
		}
	}
	
	protected function _minify()
	{
		if($this->_isMinified)
		{
			return $this->_source;
		}
		
		require_once dirname(__FILE__) . '/../_vendor/JSMin.php';
		
		//
		// minify process
		//
		
		$this->_isMinified = true;
		return $this->_source;
	}
}
